# session_manager.py
import requests
from requests import Session
from typing import Optional


class SessionManager:
    """公共 `Session` 管理对象"""

    # 初始化全局 Session
    _session: Optional[Session] = None
    _headers: dict = {
        'User-Agent':
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36 Edg/100.0.1185.39'
    }

    @classmethod
    def get_session(cls) -> Session:
        """获取全局 Session 实例"""
        if cls._session is None:
            cls._session = requests.Session()
            cls._session.headers.update(cls._headers)
        return cls._session


# if __name__ == '__main__':
#     session = SessionManager.get_session()
#     res = session.get('https://www.baidu.com')
#     res.encoding = res.apparent_encoding
#     print(res.text)
